﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using PaymentHelper;
using PaymentHelper.PaymentFlow;
using PaymentHelper.PaymentFlowTemplates.Default.Models;
using System.Web.Routing;
using PaymentHelper.Core.CommonUtils;

namespace PaymentHelper.PaymentFlowTemplates.Default.Controllers
{
    public class DefaultTemplateController:Controller
    {
        private BasePaymentFlow _paymentFlow;
        private string _systemName = "PaymentHelper.PaymentFlowTemplates.Default";

        public DefaultTemplateController()
        {
            _paymentFlow = PaymentFlowManager.GetPaymentFlowTemplateByName(_systemName);
        }

        public ActionResult OrderInfo()
        {
            LoadResources();

            ViewBag.GetShippingInfo = _paymentFlow.GetSetting("GetShippingInfo");
            ViewBag.ShowCart = _paymentFlow.GetSetting("ShowCart");

            var model = _paymentFlow.OrderPayment.BillingAddress;
            return View("PaymentHelper.PaymentFlowTemplates.Default.Views.DefaultTemplate.OrderInfo", model);
        }

        [HttpPost]
        public ActionResult OrderInfo(FormCollection frmData)
        {
            _paymentFlow.GetOrderAddressInfo(frmData);

            return RedirectToAction("Methods");
        }

        public ActionResult Methods()
        {
            LoadResources();

            ViewBag.ShowCart = _paymentFlow.GetSetting("ShowCart");

            //Get methods
            var model = _paymentFlow.GetListMethodModel();

            return View("PaymentHelper.PaymentFlowTemplates.Default.Views.DefaultTemplate.Methods", model);
        }

        [HttpPost]
        public ActionResult Methods(FormCollection frmData)
        {
            //Get selected payment method
            var methodSelected = frmData["method"];
            _paymentFlow.OrderPayment.PaymentMethodSystemName = methodSelected;

            return RedirectToAction("PaymentInfo");
        }

        public ActionResult PaymentInfo()
        {
            LoadResources();
            var model = _paymentFlow.GetPaymentInfoRoute();

            ViewBag.ShowCart = _paymentFlow.GetSetting("ShowCart");

            return View("PaymentHelper.PaymentFlowTemplates.Default.Views.DefaultTemplate.PaymentInfo", model);
        }

        [HttpPost]
        public ActionResult PaymentInfo(FormCollection frmData)
        {
            var listError = _paymentFlow.GetPaymentMethodInfo(frmData);

            if (listError != null && listError.Count > 0)
            {
                Session["PaymentInfoError"] = listError;
                return RedirectToAction("PaymentInfo");
            }

            Session["PaymentInfoError"] = null;
                        
            return RedirectToAction("Confirm");
        }

        public ActionResult Confirm()
        {
            LoadResources();
            _paymentFlow.UpdateTotalOrder();

            ViewBag.ShowCart = _paymentFlow.GetSetting("ShowCart");

            return View("PaymentHelper.PaymentFlowTemplates.Default.Views.DefaultTemplate.Confirm", _paymentFlow.OrderPayment);
        }

        [HttpPost]
        public ActionResult DoPayment()
        {
            _paymentFlow.DoPayment();

            var paymentState = PaymentManager.Instance.GetCurrentPaymentState();

            return RedirectToAction("Complete", new { tokenPayment = paymentState.TokenPayment });
        }

        [HttpGet]
        public ActionResult Complete(string tokenPayment)
        {
            var paymentState = PaymentManager.Instance.GetCurrentPaymentState();
            if (paymentState == null || paymentState.TokenPayment != tokenPayment)
            {
                string url = Url.RouteUrl("Default", ((Route)RouteTable.Routes["Default"]).Defaults);
                if (string.IsNullOrEmpty(url))
                    url = Request.Url.Authority;

                return Redirect(url);
            }

            LoadResources();
            string viewName = _systemName + ".Views.DefaultTemplate.Complete";

            return View(viewName,paymentState.Result);
        }

        public ActionResult Cancle()
        {
            string url = _paymentFlow.GetSetting("CancleUrlReturn");

            if (string.IsNullOrEmpty(url))
                return Redirect(PHUtility.GetSiteUrl());
            else
                return Redirect(url);
        }

        [ChildActionOnly]
        public ActionResult PaymentSteps(PaymentStep step)
        {
            var model = new ProcessPaymentStepModel() { PaymentStepCurrent = step };

            LoadResources();

            return PartialView("PaymentHelper.PaymentFlowTemplates.Default.Views.DefaultTemplate.PaymentSteps", model);
        }

        [ChildActionOnly]
        public ActionResult ShowCart()
        {
            LoadResources();
            string viewName = _systemName + ".Views.DefaultTemplate.ShowCart";

            ViewBag.Unit = _paymentFlow.OrderPayment.CustomerCurrency;

            return PartialView(viewName, _paymentFlow.OrderPayment.ListOrderItem);
        }

        [NonAction]
        private void LoadResources()
        {
            ViewBag.Resources = _paymentFlow.Descriptor.GetDefaultLanguage();
            ViewBag.CssClass = _paymentFlow.GetSetting("CssClassContainer");
        }
    }
}
